package com.ssd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.entity.operation.entity.SysMenu;
import com.ssd.entity.operation.entity.SysRoleMenu;
import com.ssd.entity.operation.entity.SysUser;
import com.ssd.entity.operation.vo.SysMenuVo;
import com.ssd.exception.LogicException;
import com.ssd.mapper.SysMenuMapper;
import com.ssd.mapper.SysRoleMenuMapper;
import com.ssd.service.*;
import com.ssd.utils.MenuHelper;
import com.ssd.utils.UserContext;
import com.ssd.view.TokenObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    //菜单列表
    @Override
    public List<SysMenu> findNodes() {
        //1 查询所有菜单，返回list集合
        List<SysMenu> sysMenuList = lambdaQuery().orderByDesc(SysMenu::getSortValue).list();


        if(CollectionUtils.isEmpty(sysMenuList)) {
            return null;
        }

        //2 调用工具类的方法，把返回list集合封装要求数据格式
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenuList);
        return treeList;
    }

    //菜单添加
    @Override
    public void saveAdd(SysMenu sysMenu) {
        save(sysMenu);

        //新添加子菜单，把父菜单isHalf半开状态 1
        updateSysRoleMenu(sysMenu);
    }

    //新添加子菜单，把父菜单isHalf半开状态 1
    private void updateSysRoleMenu(SysMenu sysMenu) {
        //获取当前添加菜单的父菜单
        SysMenu parentMenu = lambdaQuery().eq(SysMenu::getId, sysMenu.getParentId()).one();
        if(parentMenu != null) {
            //把父菜单isHalf半开状态 1
            sysRoleMenuService.lambdaUpdate()
                    .set(SysRoleMenu::getIsHalf, 1)
                    .eq(SysRoleMenu::getMenuId, parentMenu.getId())
                            .update();
            // 递归调用
            updateSysRoleMenu(parentMenu) ;
        }
    }



    //菜单删除
    @Override
    public void deleteById(Long id) {
        //根据当前菜单id，查询是否包含子菜单
        int count = lambdaQuery().eq(SysMenu::getParentId, id).count();

        //判断，count大于0，包含子菜单
        if(count > 0) {
            throw new LogicException("该节点下有子节点，不可以删除");
        }

        removeById(id);
    }

    //查询用户可以操作菜单
    @Override
    public List<SysMenuVo> findMenusByUserId() {
        //获取当前用户id
        TokenObject sysUser = UserContext.currentUser();
        Long userId = sysUser.getUserId();

        //根据userId查询可以操作菜单
        //封装要求数据格式，返回
        List<SysMenu> sysMenuList =
                MenuHelper.buildTree(sysMenuMapper.findMenusByUserId(userId));
        List<SysMenuVo> sysMenuVos = this.buildMenus(sysMenuList);
        return sysMenuVos;
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }

}
